package controllers;

import bean.OrderBean;
import com.avaje.ebean.Page;
import dao.OrderManager;
import models.CustomerInformation;
import models.CustomerOrder;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import java.util.Iterator;
import java.util.List;




/**
 * User: gipxy
 * Date: 4/14/12
 */
public class OrderController extends  Controller{

    final static Form<CustomerOrder> orderCustomerForm = form(CustomerOrder.class);

    public static Result index() {
        return ok("");
    }

    public static Result input() {
        CustomerOrder cusOrder = new CustomerOrder();
        cusOrder.soDonHang = "SDH" + System.currentTimeMillis();

        Form<CustomerOrder> orderCustomerForm = form(CustomerOrder.class);
        orderCustomerForm = orderCustomerForm.fill(cusOrder);

        return ok(views.html.CustomerOrder.input.render(orderCustomerForm));
    }

    public static Result allDataCustomer() {

        // [ { label: "Choice1", value: "value1" }, ... ]
        List<CustomerInformation> tasks = CustomerInformation.find.all();
        String s = "[";
        int i = 1;
        for (Iterator it = tasks.iterator(); it.hasNext();)
        {
            int size = tasks.size();
            CustomerInformation customer=(CustomerInformation)it.next();
            s = s + "{\"id\":\"" + customer.id + "\",\"label\":\"";
            s = s + String.valueOf(customer.name) +" - "+ String.valueOf(customer.address) +" - "+ String.valueOf(customer.description) + "\"";
            if( i == size){
                s= s + "}";
            }else{
                s= s + "},";
            }
            i ++;
        }
        s = s +"]";
//        System.out.println(s);
        return ok(s);
    }

    public static Result list(int page, String sortBy, String order, String filter) {
        Page<CustomerOrder> one = CustomerOrder.page(page, 10, sortBy, order, filter);
        System.out.println("aaaa" + one.getTotalRowCount());
        return ok(
                views.html.CustomerOrder.orderlist.render(one, sortBy, order, filter)
        );
    }



    public static Result save() {
        Form<OrderBean> beanForm = form(OrderBean.class).bindFromRequest();

        //Form<CustomerOrder> computerForm = new Form<CustomerOrder>();


        System.out.println("AAA: So Don Hang= " +beanForm.get().soDonHang);
        System.out.println("AAA: Ngay dat hang= " +beanForm.get().ngayDatHang);
        System.out.println("Khach Hang= " +beanForm.get().khachHang);
        System.out.println("Customer ID= " +beanForm.get().customerID);
        System.out.println("AAA productOrdersAsJson = " +beanForm.get().productOrdersAsJson);

        OrderManager.save(beanForm.get());

        return redirect(routes.OrderController.list(0, "soDonHang", "asc", ""));

        /*
        if(computerForm.hasErrors()) {
            return badRequest(views.html.CustomerOrder.input.render(computerForm));
        }
        */
//        flash("success", "CustomerOrder " + computerForm.get().name + " has been created");
//        return GO_HOME;
    }

    public static Result addProductinOrder() {
        Form<CustomerOrder> computerForm = form(CustomerOrder.class).bindFromRequest();
        if(computerForm.hasErrors()) {
            return badRequest(views.html.CustomerOrder.input.render(computerForm));
        }
        computerForm.get().save();
        return redirect(routes.OrderController.list(0, "soDonHang", "asc", ""));
    }

}
